import re


def get_tab_number(title):
    """ 判断有几个tab开头"""
    pattern2 = "^\t\t"
    if re.match(pattern2, title):
        return 2

    pattern1 = "^\t"
    if re.match(pattern1, title):
        return 1

    return 0


def build_catalog(cata):
    cata = cata.replace("\t", " ").strip(" ")
    info = cata.split(" ")
    title = " ".join(info[0:-1])
    return dict(title=title, page=int(info[-1]), children=[])



def build_tree(catalist):
    result = []
    first_level = None
    second_level = None
    third_level = None
    for cata in catalist:
        # 构建第一级目录
        if get_tab_number(cata) == 0:
            catalog = build_catalog(cata)
            first_level = catalog
            result.append(first_level)
        # 构建第二级别目录
        if get_tab_number(cata) == 1:
            catalog = build_catalog(cata)
            second_level = catalog
            children = first_level['children']
            children.append(second_level)

        # 构建第三级目录
        if get_tab_number(cata) == 2:
            catalog = build_catalog(cata)
            third_level = catalog
            children = second_level['children']
            children.append(third_level)
    return result








#
# class CatalogUtil:
#     def __init__(self, catalist):
#         self.catalist = catalist
#
#     def tree_data(self):
#         result = []
#         first_level = None
#         for cata in self.catalist:
#             if get_tab_number(cata) == 0:
#                 catalog = build_catalog(cata)
#                 first_level = catalog
#                 result.append(first_level)
#             if get_tab_number(cata) == 1:
#                 children = first_level['children']
#                 catalog = build_catalog(cata)
#                 children.append(catalog)
#
#
#         return result
